define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'luckydrawset/index' + location.search,
                   // edit_url: 'luckydrawset/edit',
                    //del_url: 'luckydrawset/del',
                    table: 'activity_luck',
                }
            });

            var table = $("#table");



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url+"&type=2",
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('标题'), operate: false,},
                        {field: 'integral', title: __('转盘消耗积分'), operate: false,},
                        {
                            field: 'status',
                            title: __('状态'),
                            searchList: {"0": __('已禁用'), "1": __('启动中')},
                            formatter: Table.api.formatter.status
                        },
                       // {field: 'detail', title: __('规则说明'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('操作'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-pencil',
                                    url: 'luckydrawset/edit?type=2',
                                    success: function () {
                                        location.reload();//刷新列表
                                    }
                                },
                                {
                                    name: 'detail',
                                    title: __('禁用'),
                                    classname: 'btn btn-xs btn-danger btn-delcfg1',
                                    icon: 'glyphicon glyphicon-remove-circle',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == '1'){
                                            return true;
                                        }
                                    }
                                },

                                {
                                    name: 'detail',
                                    title: __('启动'),
                                    classname: 'btn btn-xs btn-success btn-delcfg2',
                                    icon: 'glyphicon glyphicon-ok-circle',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == '0'){
                                            return true;
                                        }
                                    }
                                },

                            ],
                        }

                    ]
                ],
                commonSearch: false,//通用搜索
                showToggle: false,//显示隐藏列可以快速切换字段列的显示和隐藏，如果不需要此功能，可以设置showColumns: false，如果想要表格中的字段列默认隐藏可以设置字段属性visible: false即可默认隐藏
                showColumns: false,//导出按钮默认将导出整个表的所有行
                showExport: false,//导出excel
                search:false,//快速搜索
            });

            //删除配置
            $(document).on("click", ".btn-delcfg1", function () {
                var id =$(this).parent().parent().children('td').eq(1).html();
                Layer.confirm(__('确定要禁用大转盘活动吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {

                    $.post("Luckydrawset/status",{id:id},function(data,status){
                        if(data.code==1)
                        {
                            alert("操作成功");
                            location.reload();
                        }
                        else
                        {
                            alert(data.msg);
                            Layer.close(index);
                        }
                    });

                });

            });



            //删除配置
            $(document).on("click", ".btn-delcfg2", function () {
                var id =$(this).parent().parent().children('td').eq(1).html();
                Layer.confirm(__('确定要启动大转盘活动吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {

                    $.post("Luckydrawset/status",{id:id},function(data,status){
                        if(data.code==1)
                        {
                            alert("操作成功");
                            location.reload();
                        }
                        else
                        {
                            alert(data.msg);
                            Layer.close(index);
                        }
                    });

                });

            });





            var table2 = $("#table2");

            // 初始化表格
            table2.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url+"&type=1",
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'rate', title: __('权重(权重越前概率越低)'),operate: false},
                        {field: 'lunck_name', title: __('奖品名称'), operate: false},
                        {field: 'lunck_image', title: __('奖品图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'lunck_type', title: __('类型'), searchList: {"0":__('谢谢惠顾'),"1":__('积分'),"2":__('商品')}, formatter: Table.api.formatter.normal,operate: false},
                        {field: 'lunck_cost', title: __('奖品成本'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('操作'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-pencil',
                                    url: 'luckydrawset/edit?type=1',
                                    success: function () {
                                        location.reload();
                                    },
                                },
                                ]
                        }
                    ]
                ],
                commonSearch: false,//通用搜索
                showToggle: false,//显示隐藏列可以快速切换字段列的显示和隐藏，如果不需要此功能，可以设置showColumns: false，如果想要表格中的字段列默认隐藏可以设置字段属性visible: false即可默认隐藏
                showColumns: false,//导出按钮默认将导出整个表的所有行
                showExport: false,//导出excel
                search:false,//快速搜索
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Table.api.bindevent(table2);
        },


        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on('click', '.btn-success', function(){
                Form.api.submit($("form[role=form]"), function () {
                    window.parent.location.reload();
                });
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});